package com.jnu.myllm.server.impl;

import com.jnu.myllm.entity.User;
import com.jnu.myllm.mapper.UserMapper;
import com.jnu.myllm.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DataSourceTransactionManager transactionManager;

    @Override
    //登录
    public User login(String loginName, String loginPwd) {
        User user = userMapper.selectByLoginName(loginName);
        if(user==null) return null;
        // 匹配密码
        if(user.getLoginPwd().equals(loginPwd)) return user;
        return null;
    }

    @Override
    @Transactional
    //注册
    public User register(User user) {
        // 判断用户名是否存在
        if(userMapper.selectByLoginName(user.getLoginName())!=null)
            throw new RuntimeException("该用户名已被占用");
        userMapper.insert(user);
//        return userMapper.insert(user)==1?user:null;
        return userMapper.selectByLoginName(user.getLoginName());
    }
}
